#include <bits/stdc++.h>
using namespace std;
int n, m, l, a[510];
int calc(int id, int num) {
    int ans = 0;
    for (int i = id; i <= n; i += l) {
        ans += (num >= a[i] ? num - a[i] : num + m - a[i]);
    }
    return ans;
}
int dp[510][510];
int main() {
    cin >> n >> m >> l;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    memset(dp, 0x3f, sizeof(dp));
    dp[0][0] = 0;
    for (int i = 1; i <= l; i++) {
        for (int v = 0; v < m; v++) {
            int c = calc(i, v);
            for (int p = 0; p < m; p++) {
                dp[i][(p + v) % m] = min(dp[i][(p + v) % m], dp[i - 1][p] + c);
            }
        }
    }
    cout << dp[l][0];
}
